/**
 * Created by cz on 2017/5/5.
 */
import React,{Component} from "react";
import {
    View,
    Text,
    TextInput,
    StyleSheet,
    AsyncStorage,
    TouchableOpacity
}from "react-native";
const KEY = "cz";
import Toast,{DURATION} from "react-native-easy-toast"
export default class AsyncStorageTest extends Component{
    constructor(props){
        super(props);
    }
    onSave(){
        AsyncStorage.setItem(KEY,this.text,(error)=>{
            if(!error){
                this.toast.show("保存成功",DURATION.LENGTH_LONG);
            }else{
                this.toast.show("保存失败",DURATION.LENGTH_LONG);
            }
        })
    }
    reMove(){
        AsyncStorage.removeItem(KEY,(error)=>{
            if(!error){
                this.toast.show("删除成功",DURATION.LENGTH_LONG);
            }else{
                this.toast.show("删除失败",DURATION.LENGTH_LONG);
            }
        })
    }
    getData(){
        AsyncStorage.getItem(KEY,(error,result)=>{
            if(!error){
                if(result !== "" && result !== null){
                    this.toast.show("取出的值是:"+result,DURATION.LENGTH_LONG);
                }else{
                    this.toast.show("取出的内容不存在",DURATION.LENGTH_LONG);
                }
            }else{
                this.toast.show("取出失败",DURATION.LENGTH_LONG);
            }
        })
    }
    render(){
        return (
            <View style={styles.container}>
                <TextInput style={{borderWidth:1,height:40}} onChangeText={(text)=>this.text=text}/>
                <View style={{flexDirection:"row",justifyContent:"space-between",alignItems:"center"}}>
                    <TouchableOpacity onPress={()=>this.onSave()}>
                        <Text style={styles.tips}>保存</Text>
                    </TouchableOpacity>
                    <TouchableOpacity onPress={()=>this.reMove()}>
                        <Text style={styles.tips}>移除</Text>
                    </TouchableOpacity>
                    <TouchableOpacity onPress={()=>this.getData()}>
                        <Text style={styles.tips}>取出</Text>
                    </TouchableOpacity>
                </View>
                <Toast ref={toast=>this.toast=toast}/>
            </View>
        )
    }
}
const styles = StyleSheet.create({
    container:{
        flex:1
    },
    tips:{
        fontSize:20
    }
});